import { defineConfig,loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import path from 'path';
import {visualizer} from 'rollup-plugin-visualizer'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'


// https://vitejs.dev/config/
export default defineConfig(({ mode })=>{
  const config = loadEnv(mode, './')
  return {
    plugins: [
      vue(), visualizer({
      emitFile: false,
      filename: 'analysis-chart.html', // 分析图生成的文件名
      open: true, // 如果存在本地服务端口，将在打包后自动展示
    }),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
        '*': path.resolve(''),
      },
    },
    server: {
      open: true,
      hmr:true,
      proxy: {
        '/api': {
          target: config.VITE_BASE_URL, //目标url
          changeOrigin: true, //支持跨域
          rewrite: (path) => path.replace(/^\/api/, ""),
          //重写路径,替换/api
        }
      }
    },
  }
});
